package git.soulbgm.mapreduce.writable;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author SoulBGM
 * @date 2021/11/17 23:55
 */
public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean> {

    private Text outKey = new Text();
    private FlowBean outValue = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();

        // 格式:
        // 1	13736230513	192.196.100.1	www.atguigu.com	2481	24681	200
        // 2	13846544121	192.196.100.2			264	0	200
        // 按一个或多个空格分隔字符
        String[] split = line.split("\\s+");

        outKey.set(split[1]);
        outValue.setUpFlow(Long.parseLong(split[split.length - 3]));
        outValue.setDownFlow(Long.parseLong(split[split.length - 2]));
        context.write(outKey, outValue);
    }
}
